package cn.itcast.hotel;

import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.client.*;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;

import static cn.itcast.hotel.constants.HotelConstants.MAPPING_TEMPLATE;


@SpringBootTest
public class Hotelindex {

    private RestHighLevelClient client;


    @Test
    void contextLoads() {

    }



    @Test
    //添加
    public void puttest() throws IOException {
        CreateIndexRequest create = new CreateIndexRequest("hotel");
        create.source(MAPPING_TEMPLATE, XContentType.JSON);
        client.indices().create(create, RequestOptions.DEFAULT);
    }
    @Test
    //删除
    public void deletetest() throws IOException {
        GetIndexRequest hotel = new GetIndexRequest("hotel");
        boolean exists = client.indices().exists(hotel, RequestOptions.DEFAULT);
        System.out.println(exists?"正确":"错误");
        if(exists){
            DeleteIndexRequest hotel1 = new DeleteIndexRequest("hotel");
            client.indices().delete(hotel1,RequestOptions.DEFAULT);
            System.out.println("删除成功");
        }else{
            System.out.println("不存在");
        }

    }



    @BeforeEach
    public void init() {
        this.client = new RestHighLevelClient(RestClient.builder(
                HttpHost.create("http://42.192.6.166:9200")
        ));
    }



}
